﻿using Aliyun.Acs.Core.Http;
using DocumentFormat.OpenXml.Wordprocessing;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// API基类
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/" + ApiVersion.Version)]
    public class BaseController<T> : ControllerBase where T : BaseController<T>
    {
        /// <summary>
        /// 日志
        /// </summary>
        protected readonly ILogger<T> Logger;
        protected readonly IRepository<ZmSystemLog> _systemLogRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="systemLogRepository"></param>
        public BaseController(ILogger<T> logger, IRepository<ZmSystemLog> systemLogRepository)
        {
            Logger = logger;
            _systemLogRepository = systemLogRepository;
        }

        protected void WriteSystemLog(string actionName, RespRet respRet,object param)
        {
            string usernumber = this.CurrentUser();

            //if (usernumber == null)
            //{
            //    usernumber = "";
            //}
            //    var dmSystemLog = new ZmSystemLog();
            //    dmSystemLog.Id = SequentialGuid.SequentialGuidGenerator.Instance.NewGuid().ToString("N");
            //    dmSystemLog.UserId = usernumber;
            //    dmSystemLog.ActionName = actionName;
            //    dmSystemLog.CreateOn = DateTime.Now;
            //    dmSystemLog.ModifiedOn = DateTime.Now;
            //    dmSystemLog.Path = Request.Path;
            //    dmSystemLog.LoginType = Request.Headers["User-Agent"];
            //    //if (dmSystemLog.LoginType != null)
            //    //{
            //    //    if (dmSystemLog.LoginType.Length > 255)
            //    //    {
            //    //        dmSystemLog.LoginType = dmSystemLog.LoginType.Substring(0, 255);
            //    //    }
            //    //}
            //    dmSystemLog.Param= JsonConvert.SerializeObject(param);
            //    dmSystemLog.LogTime = DateTime.Now;
            //    dmSystemLog.Ip = this.IpAddress();
            //    dmSystemLog.Respret = JsonConvert.SerializeObject(respRet);
            //    _systemLogRepository.Insert(dmSystemLog);
            //    _systemLogRepository.SaveChanges();
            ////}
        }
        protected void WriteSystemLogError(string actionName, RespRet respRet, object param)
        {
            string usernumber = this.CurrentUser();
            if (usernumber != null)
            {
                var dmSystemLog = new ZmSystemLog();
                dmSystemLog.Id = SequentialGuid.SequentialGuidGenerator.Instance.NewGuid().ToString("N");
                dmSystemLog.UserId = usernumber;
                dmSystemLog.ActionName = actionName;
                dmSystemLog.CreateOn = DateTime.Now;
                dmSystemLog.ModifiedOn = DateTime.Now;
                dmSystemLog.Path = Request.Path;
                dmSystemLog.LoginType = Request.Headers["User-Agent"];
                //if (dmSystemLog.LoginType != null)
                //{
                //    if (dmSystemLog.LoginType.Length > 255)
                //    {
                //        dmSystemLog.LoginType = dmSystemLog.LoginType.Substring(0, 255);
                //    }
                //}
                dmSystemLog.Param = JsonConvert.SerializeObject(param);
                dmSystemLog.LogTime = DateTime.Now;
                dmSystemLog.Ip = this.IpAddress();
                dmSystemLog.Respret = JsonConvert.SerializeObject(respRet);
                dmSystemLog.ErrorStack = "error";
                _systemLogRepository.Insert(dmSystemLog);
                _systemLogRepository.SaveChanges();
            }
        }
    }
}
